#!/usr/bin/env python
# -*- coding: utf-8 -*-

from core.utils.singleton import singleton
from core.ui.mainframe import MainFrame
import wx
import wx.lib.agw.aui as aui
from core.ide.themesmanager import ThemesManager
from core.ui.menu import Menu
from core.ide.commandmanager import CommandManager
#TODO à amiliorer pour ajouter toute les possibilité

@singleton
class ToolsBarManager:
    def __init__(self):
        self.toolsbars={}
        self.dropMenu={}
        
    def createToolsBar(self,name,*tools):
        if self.toolsbars.has_key(name):
            tb=self.toolsbars[name]
        else:
            tb=ToolsBar(MainFrame())
            tb.quickToolBar(*tools)
            self.toolsbars[name]=tb
            MainFrame().addPane(tb, aui.AuiPaneInfo().Name(name).Caption(name).ToolbarPane().Top())
        return tb

 
class ToolsBar(aui.AuiToolBar):
    def __init__(self,parent):
        aui.AuiToolBar.__init__(self,parent, -1, wx.DefaultPosition, wx.DefaultSize,agwStyle=aui.AUI_TB_DEFAULT_STYLE  | aui.AUI_TB_PLAIN_BACKGROUND)
        self.items={}

    def quickToolBar(self,*tools):
        self.SetToolBitmapSize(wx.Size(16, 16))
        options=None
        id_=wx.GetCurrentId()
        for ind in tools:
            item=None
            if type(ind) is list:
                options=ind[1:]
                ind=ind[0]
                
                
            if isinstance(ind,wx.Object):
                item=self.AddControl(ind)
                ind=ind.GetId()
            elif self.items.has_key(ind):
                return self.items[ind]
                
            elif ind[0]=="|":
                item=self.AddSeparator()
                continue
            elif options:
                id_+=1
                choice = wx.ComboBox(self, -1,options[0], choices=options, style=wx.CB_READONLY)
                item=self.AddControl(choice)
            elif type(ind) is str:
                item=self.AddSimpleTool(-1, ind, ThemesManager().getDefaultTheme().getImage(ind,'icone_16'),ind)
            #elif type(ind) is list and len(ind) and type(ind[0]) is str:
            #    choice = wx.ComboBox(tb, -1,ind[0], choices=ind, style=wx.CB_READONLY)
            #    tb.AddControl(choice)
            self.items[ind]=ToolBarItem(item,ind)
            toolItem=self.items[ind]
        self.Realize()
        return toolItem
    


class ToolBarItem:
    def __init__(self,toolBarItem,name):
        self.toolBarItem=toolBarItem
        self.id=self.toolBarItem.GetId()
        MainFrame().Bind(wx.EVT_TOOL, self.onToolClick, id=self.id)
        self.name=name
        self.cmd=None
        self.args=None

    def setCommand(self,cmd,*args):
        self.cmd=cmd
        self.args=args
        
    def setPosition(self,pos):
        pass
    
    def onToolClick(self,e):
        if self.cmd and  self.args:
            CommandManager().execute(self.cmd,*self.args)
        elif self.cmd: 
            CommandManager().execute(self.cmd)